<?php

namespace App\Jobs;

use App\Models\Contract;
use App\Models\NewSearch;
use App\Models\OpenSearch;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use OSS\OssClient;

class ThumbTask implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $id;
    private $thumb;
    public $tries = 2;
    public $timeout = 30;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($id,$thumb)
    {
        $this->id = $id;
        $this->thumb = $thumb;
        //因为要导入大文件 需要很久时间
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $config = [
            'accessId' => config('site.accessKeyId'),
            'accessKey' => config('site.accessSecret'),
            'endpoint' => config('site.point'),
            'bucket' => config('site.bucket')
        ];
        $ossClient = new OssClient($config['accessId'], $config['accessKey'], $config['endpoint']);
        $thumb_path = 'thumb/'.uniqid().'.jpg';
        $result = $ossClient->uploadFile($config['bucket'],$thumb_path, public_path($this->thumb));
        $model = Contract::where('id',$this->id)->first();
        if ($result['info']) {
            $model->title_pic = $result['info']['url'];
            $model->is_finish = 1;
            $search = new NewSearch();
            $upOpen[] = array('id'=>$model->id,'title'=>$model->title,'title_pic'=>$model->title_pic);
            $search->add($upOpen);
        }

        $model->save();
    }
}
